package com.lala.rennigou.controller;

import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonAlias;
import com.lala.rennigou.Util.SecurityUtils;
import com.lala.rennigou.mapper.EasybuyUserMapper;
import com.lala.rennigou.pojo.EasybuyNews;
import com.lala.rennigou.pojo.EasybuyProduct;
import com.lala.rennigou.pojo.EasybuyUser;
import com.lala.rennigou.service.EasybuyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.util.*;


import java.util.Date;
import java.util.HashMap;


import java.util.concurrent.TimeUnit;

@RestController
public class EasybuyUserController {
    @Autowired
    private EasybuyUserService easybuyUserService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    public EasybuyUser yanzheng(HttpServletRequest request){
        EasybuyUser easybuyUser=null;
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                    if(map!=null){
                        easybuyUser=(EasybuyUser)map.get("user");
                    }
                }
            }
        }
        return easybuyUser;
    }

    @RequestMapping("/login")
    private Object login(String loginName, String password, HttpServletResponse response){
        EasybuyUser easybuyUser=easybuyUserService.login(loginName);
        if (easybuyUser==null){
            return JSONArray.toJSONString("ufalse");
        }else {
            if (easybuyUser.getPassword().equals(SecurityUtils.md5Hex(password))){
                Map<String,Object>map=new HashMap<>();
                map.put("user",easybuyUser);
                Date date=new Date();
                map.put("date",date.getTime());
                String id=UUID.randomUUID().toString();
                Cookie cookie=new Cookie("user", id);
                cookie.setMaxAge(60*30);
                response.addCookie(cookie);
                redisTemplate.opsForValue().set(id,map,30, TimeUnit.MINUTES);
                return JSONArray.toJSONString("true");
            }else {
                return JSONArray.toJSONString("pwdfalse");
            }
        }
    }

    @RequestMapping("/user")
    public Object user(HttpServletRequest request){
        EasybuyUser easybuyUser=null;
        Date date=new Date();
        long l=date.getTime();
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie.getValue());
                   if(map!=null){
                       easybuyUser=(EasybuyUser)map.get("user");
                       long minutes = ((l-(long)map.get("date")) / 1000) / 60;
                       if (minutes>=25){
                           Map<String,Object>param=new HashMap<>();
                           param.put("user",easybuyUser);
                           Date d=new Date();
                           param.put("date",d.getTime());
                           redisTemplate.opsForValue().set(cookie.getValue(),param);
                       }
                   }
                }
            }
        }

        return JSONArray.toJSON(easybuyUser);
    }
    @RequestMapping("/zhuxiao")
    public Object zhuxiao(HttpServletRequest request){
        Cookie[] cookies =  request.getCookies();//获取保存在request的所有cookie
        if(cookies != null){//判断cookies数组是否为空
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("user")){//通过for循环找到想要获取的值
                    redisTemplate.delete(cookie.getValue());
                    Map<String,Object> res = new HashMap<>();
                    res.put("msg",true);
                    return JSONArray.toJSONString(res);
                }
            }
        }
        return JSONArray.toJSONString("true");
    }

    @RequestMapping(value = "/regist")
    public Object zhuce(@RequestBody  @RequestParam Map<String,String> map){
        System.out.println(map);
        EasybuyUser easybuyUser = new EasybuyUser();
        easybuyUser.setLoginname(map.get("loginName"));
        easybuyUser.setUsername(map.get("username"));
        easybuyUser.setPassword(SecurityUtils.md5Hex(map.get("password")));
        easybuyUser.setEmail(map.get("email"));
        easybuyUser.setMobile(map.get("mobile"));
        if(!map.get("identitycode").equals("")){
            easybuyUser.setIdentitycode(map.get("identitycode"));
        }
        String loginName=map.get("loginName");
        String yz=map.get("yanz");
        Integer yzm=(Integer) redisTemplate.opsForValue().get(loginName+"yzm");
        if (yzm==null){
            return JSONArray.toJSON("yanzm");
        }
        if (yzm!=Integer.parseInt(yz)){
            return JSONArray.toJSON("yanz");
        }
        int a = easybuyUserService.insert(easybuyUser);
        //int a = 1;

        if (a>0){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }

    @RequestMapping(value = "/selectByName")
    public Object selectByName(String loginName){
        EasybuyUser easybuyUser = easybuyUserService.login(loginName);
        if (easybuyUser==null){
            return JSONArray.toJSON("true");
        }else {
            return JSONArray.toJSON("false");
        }
    }


    @RequestMapping(value = "/userfenye")
    public Object userfenye(int pagenum){
        return easybuyUserService.selectAll(pagenum,8);
    }



    @RequestMapping(value = "/deleteById")
    public Object deleteById(int id){
        int a = easybuyUserService.deleteById(id);
        String b = "false";
        if(a>0){
            b = "true";
        }
        return b;
    }

    @RequestMapping(value = "/selectById")
    public Object selectById(int id){
        EasybuyUser easybuyUser = easybuyUserService.selectById(id);
        return easybuyUser;
    }


    @RequestMapping("/dizhi")
    private Object dizhi(HttpServletRequest request){
        EasybuyUser easybuyUser=yanzheng(request);
       if (easybuyUser!=null){
           EasybuyUser easybuyUser1 = easybuyUserService.seleceByAdress(easybuyUser.getId());
           if (easybuyUser1==null){
               return JSONArray.toJSON(easybuyUser);
           }else return JSONArray.toJSON(easybuyUser1);
       }
       return JSONArray.toJSON(easybuyUser);
    }

    @RequestMapping("/updateById")
    public Object updateById(EasybuyUser easybuyUser){
        /*System.out.println(map);
        EasybuyUser easybuyUser = new EasybuyUser();
        easybuyUser.setId(Integer.parseInt(map.get("id")));
        easybuyUser.setLoginname(map.get("loginName"));
        easybuyUser.setUsername(map.get("username"));
        if(!map.get("password").equals("")){
            easybuyUser.setPassword(SecurityUtils.md5Hex(map.get("password")));
        }
        easybuyUser.setEmail(map.get("email"));
        easybuyUser.setMobile(map.get("mobile"));
        easybuyUser.setSex(Integer.parseInt(map.get("sex")));
<<<<<<< HEAD
        //easybuyUser.setType(Integer.parseInt(map.get("type")));
=======
//        easybuyUser.setType(Integer.parseInt(map.get("type")));
>>>>>>> f84db59527d4d8ee3d56b51c101badb5c5a0d9a8
        if(!map.get("identitycode").equals("")){
            easybuyUser.setIdentitycode(map.get("identitycode"));
        }
        System.out.println(easybuyUser);*/
        int a = easybuyUserService.update(easybuyUser);
        if(a>0){
            return JSONArray.toJSON("true");
        }
        return JSONArray.toJSON("false");
    }


    @RequestMapping("/usershowI")
    public Object usershowI(HttpServletRequest request){
        return JSONArray.toJSON(getUser(request));
    }


    @RequestMapping("/showshoucang")
    public Object showshoucang(HttpServletRequest request){
        EasybuyUser easybuyUser = getUser(request);
        List<EasybuyProduct> list = (List<EasybuyProduct>) redisTemplate.opsForValue().get(easybuyUser.getLoginname());
        System.out.println(list);
        return list;
    }

    @RequestMapping("/deleteSC")
    public Object deleteSC(HttpServletRequest request,String id){
        EasybuyUser easybuyUser = getUser(request);
        List<EasybuyProduct> list = (List<EasybuyProduct>) redisTemplate.opsForValue().get(easybuyUser.getLoginname());
        int a = 0;
        for (EasybuyProduct easybuyProduct : list){
            if(id.equals(easybuyProduct.getId())){
                list.remove(a);
                break;
            }
            a++;
        }
        redisTemplate.opsForValue().set(easybuyUser.getLoginname(),list);
        return JSONArray.toJSON("true");
    }




    public EasybuyUser getUser(HttpServletRequest request){
        EasybuyUser easybuyUser = null;
        Cookie[] cookie = request.getCookies();//获取保存在request的所有cookie
        if(cookie!=null){
            for(Cookie cookie1 : cookie){
                if(cookie1.getName().equals("user")){
                    Map<String ,Object> map=(Map<String ,Object>) redisTemplate.opsForValue().get(cookie1.getValue());
                    if(map!=null){
                        easybuyUser=(EasybuyUser)map.get("user");
                    }
                }
            }
        }
        return easybuyUser;
    }




}
